package conn

import (
 	"##PROJECT##/config/vars"
 	"strconv"

 	"github.com/go-redis/redis"
 )

var redisclient *redis.Client = nil

func Redis() *redis.Client {
	dbconf := vars.Redis
	host := dbconf.Host
	port := dbconf.Port
	auth := dbconf.Auth
	db := dbconf.Db

	addr := host + ":" + strconv.FormatInt(int64(port), 10)

	if redisclient != nil && redisclient.Ping().Err() != nil {
		redisclient.Close()
		redisclient = nil
	}

	if redisclient == nil {
		redisclient = redis.NewClient(&redis.Options{
			Addr:     addr,
			Password: auth,
			DB:       db,
		})
	}

	return redisclient
}
